summaryrefslogtreecommitdiff
path: root/src/pages/google_merchant/products/[page].js
blob: 7db826bb87dc07b27ba89e0bde776d9b323bd645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { createSlug } from '@/core/utils/slug'
import toTitleCase from '@/core/utils/toTitleCase'
import productSearchApi from '@/lib/product/api/productSearchApi'
import _ from 'lodash-contrib'
import { create } from 'xmlbuilder'

export async function getServerSideProps({ res, query }) {
  const titleContent = 'Indoteknik.com: B2B Industrial Supply & Solution'
  const descriptionContent =
    'Temukan pilihan produk B2B Industri & Alat Teknik untuk Perusahaan, UMKM & Pemerintah dengan lengkap, mudah dan transparan.'

  const { page } = query
  const limit = 5000
  const queries = {
    limit,
    page: page.replace('.xml', ''),
    priceFrom: 1,
    orderBy: 'popular',
    fq: 'image_s:["" TO *]'
  }
  const products = await productSearchApi({ query: _.toQuery(queries) })

  const productItems = []
  products.response.products.forEach((product) => {
    const productUrl = createSlug('/shop/product/', product.name, product.id, true)
    const productId = product.code != '' ? product.code : product.id
    const productDescription = product.description?.replace(/<[^>]*>/g, ' ') || ''

    const item = {
      'g:id': { '#text': productId },
      'g:title': { '#text': `<![CDATA[${toTitleCase(product.name)}]]>` },
      'g:description': { '#text': `<![CDATA[${productDescription}]]>` },
      'g:link': { '#text': productUrl },
      'g:image': { '#text': product.image },
      'g:condition': { '#text': 'new' },
      'g:availability': { '#text': 'in_stock' },
      'g:brand': { '#text': product.manufacture?.name || '' },
      'g:price': { '#text': `${product.lowestPrice.price} IDR` }
    }
    if (product.lowestPrice.discountPercentage > 0) {
      item['g:sale_price'] = { '#text': `${product.lowestPrice.priceDiscount} IDR` }
    }
    productItems.push(item)
  })

  const googleMerchant = {
    rss: {
      '@xmlns:g': 'http://base.google.com/ns/1.0',
      '@version': '2.0',
      channel: {
        title: { '#text': `<![CDATA[${titleContent}]]>` },
        link: { '#text': process.env.SELF_HOST },
        description: { '#text': `<![CDATA[${descriptionContent}]]>` },
        item: productItems
      }
    }
  }

  res.setHeader('Content-Type', 'text/xml;charset=iso-8859-1')
  res.write(create(googleMerchant).end())
  res.end()

  return { props: {} }
}

export default function GoogleMerchantPage() {
  return null
}